<?php
namespace app\onlineRepair\controller;
use app\common\Base;
use think\Db;

class OnlineRepairReg extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$where["报修人ID"] = appUser()["ID"];
		$termId = input("termId");
		$page = input("page");
		$rows = input("rows");
		if($termId) $where["学期ID"] = $termId;
		$startDate = input("startDate");
		$endDate = input("endDate");
		if($startDate) $startDate = $startDate.' 00:00:00';
		if($endDate) $endDate = $endDate.' 23:59:59';
		if($startDate && $endDate) {
			$where['报修时间']=array('between',array($startDate,$endDate));
		}else {
			if($startDate) $where["报修时间"] = array("egt", $startDate);
			if($endDate) $whereTime["报修时间"] = array("elt", $endDate);
		}
		$result = Db::table('网上报修')->alias('a')
				  ->join('学期名称 b','a.学期ID=b.ID','LEFT')
				  ->field("a.ID,报修人,联系电话,类别ID,报修类别,建筑物,楼层,报修地点,故障描述,报修时间,维修人,维修回复,回复时间,维修状态,学期ID,报修人ID,维修人员ID,维修人名字,维修人手机号,报修类别,学期名称")
				  ->where($where)
				  ->order("ID desc")
				  ->page($page, $rows)
		          ->select();
		$count = Db::table('网上报修')->alias('a')
				  ->join('学期名称 b','a.学期ID=b.ID','LEFT')
				  ->where($where)
				  ->count();
		$grid['rows'] = selectResult($result);
        $grid['total'] = $count;
		return $grid;
	}

	public function addUpdate() {
		$param = request()->param();
		try {
			if(array_key_exists("ID", $param)) {
				update('网上报修', $param);
			}else {
				$param["学期ID"] = currTerm()["termId"];
				$param["报修人"] = appUser()["姓名"];
				$param["报修人ID"] = appUser()["ID"];
				$param["联系电话"] = appUser()["手机号"];
				$param["维修状态"] = '001';
				insert('网上报修', $param);
			}
			return '000';
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return '999';
	}

	public function del() {
		$delIds = input("delIds");
		try {
			Db::name('网上报修')->where('ID','in',explode(",", $delIds))->delete();
			return "000";
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		return "999";
	}

}
